package com.itheima.health.task;

import com.itheima.health.controller.AliyunOSSController;
import com.itheima.health.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author 山外小王
 * @Description
 * @Date 2024/04/16/20:33
 */
@Component
@Slf4j
public class OrderTask {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Scheduled(fixedRate = 1000 * 3600)
    public void cleanImages() {
        log.info("开始清理垃圾图片");
        Set<String> allImgFile = redisTemplate.opsForSet().members("ALLImgFile");
        assert allImgFile != null;
        List<String> collect = allImgFile.stream().map(s -> {
            String[] split = s.split("/");
            return split[split.length - 1];
        }).collect(Collectors.toList());
//        List<String> collect = allImgFile.stream()
//                .map(s -> s.split("/"))
//                .map(arr -> arr.length>=3 ?arr[2]:"")
//                .collect(Collectors.toList());
        if (collect.size() > 0) {
            log.info("存在垃圾图片:{}",collect);
            aliOSSUtils.deleteByList(collect);
        }
        log.info("暂无垃圾图片");
        if (Boolean.TRUE.equals(redisTemplate.hasKey(AliyunOSSController.ALL_IMG_FILE))) {
            redisTemplate.delete(AliyunOSSController.ALL_IMG_FILE);
        }
    }
}
